const gulp = require("gulp"),
	babel = require("gulp-babel"),
	minifycss = require("gulp-clean-css"),
	htmlmin = require("gulp-htmlmin"),
	uglify = require("gulp-uglify"),
	sass = require("gulp-sass"),
	connect = require("gulp-connect");

gulp.task("html",()=>{
	gulp.src("./index.html")
	.pipe(htmlmin({collapseWhitespace:true}))
	.pipe(gulp.dest("dist"))
	.pipe(connect.reload())
});

gulp.task("html-html",()=>{
	gulp.src("html/**/*.html")
	.pipe(htmlmin({collapseWhitespace:true}))
	.pipe(gulp.dest("dist/html"))
	.pipe(connect.reload())
});

gulp.task("css",()=>{
	gulp.src("scss/**/*.scss")
	.pipe(sass({outputStyle:"expanded"}))
	.pipe(minifycss())
	.pipe(gulp.dest("dist/css"))
	.pipe(connect.reload())
});

gulp.task("js",()=>{
	gulp.src("js/*.js")
	.pipe(babel({
           presets: ['env']
        }))
	.pipe(uglify())
	.pipe(gulp.dest("dist/js"))
	.pipe(connect.reload())
});

gulp.task("imgs-copy",()=>{
	gulp.src("imgs/**/*.*")
	.pipe(gulp.dest("dist/imgs"))
	.pipe(connect.reload())
});

gulp.task("lib-copy",()=>{
	gulp.src("lib/**/*.*")
	.pipe(gulp.dest("dist/lib"))
	.pipe(connect.reload())
});

gulp.task("api-copy",()=>{
	gulp.src("api/*.php")
	.pipe(gulp.dest("dist/api"))
	.pipe(connect.reload())
});

gulp.task("conn",()=>{
	connect.server({
		root:"dist",
		livereload:true
	});
});

gulp.task("watch",()=>{
	gulp.watch("index.html",["html"]);
	gulp.watch("html/**/*.html",["html-html"]);
	gulp.watch("scss/**/*.scss",["css"]);
	gulp.watch("js/**/*.js",["js"]);
	gulp.watch("imgs/**/*.*",["imgs-copy"]);
	gulp.watch("lib/**/*.*",["lib-copy"]);
	gulp.watch("api/*.php",["api-copy"]);
});

gulp.task("default",["html","html-html","css","js","imgs-copy","lib-copy","api-copy","conn","watch"]);